//
//  SupToolsViewController.m
//  UEAssistant
//
//  Created by mac on 2017/8/2.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "SupToolsViewController.h"

@interface SupToolsViewController ()<UITableViewDelegate,UITableViewDataSource>{

    NSArray                     *_dataSource;//数据源
    NSArray                     *_titleArr;
    NSMutableArray                     *_tools;
}

@property (nonatomic, strong) UITableView           *tableView;
@property (nonatomic, strong) UISwitch              *swichY;
@property (nonatomic, strong) UISwitch              *swichB;
@property (nonatomic, strong) UISwitch              *swichC;

@end

@implementation SupToolsViewController

- (void)viewWillAppear:(BOOL)animated{
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self LoadData];
    [self layoutUI];
}

- (void)layoutUI{
    
    self.title = @"机器翻译";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
}

- (void)LoadData{
    
    _titleArr = @[@"有道翻译",@"百度翻译"];
    _dataSource = @[@"专业机器翻译（新闻传媒）",@"专业机器翻译（法律合同）",@"专业机器翻译（旅游口语）",@"专业机器翻译（国际工程）",@"专业机器翻译（金融财经）",@"专业机器翻译（专利产权）",@"专业机器翻译（IT通讯）",@"专业机器翻译（装备制造）",@"专业机器翻译（石油化工）"];
    _tools = [NSMutableArray arrayWithArray:[detailsModel.memtool_id componentsSeparatedByString:@","]];
}


#pragma mark -- <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 2;
    }
    return _dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40*Radio;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    

    return 0.001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 30*Radio)];
        backView.backgroundColor = RGB(247, 247, 247, 1);
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15*Radio, 15*Radio, 300, 20)];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = RGB(153, 153, 153, 1);
        label.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
        label.text = @"免费引擎（仅在翻译界面使用）";
        [backView addSubview:label];
        return backView;
    }
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 30*Radio)];
    backView.backgroundColor = RGB(247, 247, 247, 1);
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15*Radio, 15*Radio, 300, 20)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = RGB(153, 153, 153, 1);
    label.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
    label.text = @"收费引擎（10马刀/千字，限时1折，仅用于预翻）";
    [backView addSubview:label];
    return backView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *view = nil;
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.textColor = RGB(51, 51, 51, 1);
    if (indexPath.section == 0) {
        cell.textLabel.text = _titleArr[indexPath.row];

        if (indexPath.row == 0) {
            _swichY = [[UISwitch alloc]initWithFrame:CGRectMake(Screen_Width-70*Radio, 15.0f, 100.0f, 30.0f)];
            if ([_tools containsObject:@"-1"]) {
                _swichY.on = YES;
            }else{
                _swichY.on = NO;
            }
            [_swichY addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            _swichY.onTintColor = mainColor;
            _swichY.tag = 500;
            [cell addSubview:_swichY];
        }else{
            _swichB = [[UISwitch alloc]initWithFrame:CGRectMake(Screen_Width-70*Radio, 15.0f, 100.0f, 30.0f)];
            if ([_tools containsObject:@"-5"]) {
                _swichB.on = YES;
            }else{
                _swichB.on = NO;
            }
            [_swichB addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            _swichB.onTintColor = mainColor;
            _swichB.tag = 600;
            [cell addSubview:_swichB];
        }

    }else{
        cell.textLabel.text = _dataSource[indexPath.row];
        _swichC = [[UISwitch alloc]initWithFrame:CGRectMake(Screen_Width-70*Radio, 15.0f, 100.0f, 30.0f)];
        if ([_tools containsObject:@"-4"]) {
            _swichC.on = YES;
        }else{
            _swichC.on = NO;
        }
        [_swichC addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        _swichC.onTintColor = mainColor;
        _swichC.tag = 700;
        [cell addSubview:_swichC];

    }

    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)switchAction:(UISwitch *)sender{

    BOOL isButtonOn = [sender isOn];
    NSString *IDString;
    
    switch (sender.tag) {
        case 500:
        {
            if (isButtonOn) {
                [_tools addObject:@"-1"];
                IDString = [_tools componentsJoinedByString:@","];
            }else{
                [_tools removeObject:@"-1"];
                IDString = [_tools componentsJoinedByString:@","];

            }
            NSDictionary *params = @{@"projectId":shareData.product_Id,
                                     @"memId":IDString};
            [AvailableNetWorking postWithUrl:saveTransToolsInfo_connector params:params successHandler:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    
                    [TopAlertView SetUpbackgroundColor:mainColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:@"操作成功" andTitleColor:[UIColor whiteColor] andCancel:NO];
                    detailsModel.memtool_id = IDString;
                }
                
            } falureHandler:^(NSError *error) {
                
            }];
        }
            break;
        case 600:
        {
            if (isButtonOn) {
                [_tools addObject:@"-5"];
                IDString = [_tools componentsJoinedByString:@","];
            }else{
                [_tools removeObject:@"-5"];
                IDString = [_tools componentsJoinedByString:@","];
                
            }
            NSDictionary *params = @{@"projectId":shareData.product_Id,
                                     @"memId":IDString};
            [AvailableNetWorking postWithUrl:saveTransToolsInfo_connector params:params successHandler:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    
                    [TopAlertView SetUpbackgroundColor:mainColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:@"操作成功" andTitleColor:[UIColor whiteColor] andCancel:NO];
                    detailsModel.memtool_id = IDString;
                }
                
            } falureHandler:^(NSError *error) {
                
            }];

        }
            break;
        case 700:
        {
            if (isButtonOn) {
                [_tools addObject:@"-4"];
                IDString = [_tools componentsJoinedByString:@","];
            }else{
                [_tools removeObject:@"-4"];
                IDString = [_tools componentsJoinedByString:@","];
                
            }
            NSDictionary *params = @{@"projectId":shareData.product_Id,
                                     @"memId":IDString};
            [AvailableNetWorking postWithUrl:saveTransToolsInfo_connector params:params successHandler:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [TopAlertView SetUpbackgroundColor:mainColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:@"操作成功" andTitleColor:[UIColor whiteColor] andCancel:NO];

                    detailsModel.memtool_id = IDString;
                    
                }
                
            } falureHandler:^(NSError *error) {
                
            }];

        }
            break;
        default:
            break;
    }
} 

#pragma mark -- getter

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = RGB(247, 247, 247, 1);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = RGB(247, 247, 247, 1);
        _tableView.rowHeight = 60*Radio;

    }
    return _tableView;
}

@end
